Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run rust-fmt On Doc Comments #104058

Closed

Conversation

AldaronLau
Copy link
Contributor

Redo of #102285, but only for /library

@rustbot
Copy link
Collaborator

rustbot commented Nov 6, 2022

r? @scottmcm

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 6, 2022
@rustbot
Copy link
Collaborator

rustbot commented Nov 6, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-log-analyzer
Copy link
Collaborator

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
configure: rust.debug-assertions := True
configure: rust.overflow-checks := True
configure: llvm.assertions      := True
configure: dist.missing-tools   := True
configure: build.configure-args := ['--enable-sccache', '--disable-manage-submodu ...
configure: writing `config.toml` in current directory
configure: 
configure: run `python /checkout/x.py --help`
Attempting with retry: make prepare
---
 Documenting core v0.0.0 (/checkout/library/core)
error: unresolved link to `File`
   --> library/core/src/primitive_docs.rs:69:1
    |
69  | / /// The `!` type, also called "never".
70  | | ///
71  | | /// `!` represents the type of computations which never resolve to any value at all. For example,
72  | | /// the [`exit`] function `fn exit(code: i32) -> !` exits the process without ever returning, and
...   |
264 | | /// [`Debug`]: fmt::Debug
265 | | /// [`default()`]: Default::default
    |
    |
    = note: `-D rustdoc::broken-intra-doc-links` implied by `-D warnings`
    = note: the link appears in this line:
            
            Default` for (eg.) [`File`] by just making [`default()`] panic.)
    = note: no item named `File` in scope
    = note: no item named `File` in scope
    = help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
error: unresolved link to `default`
   --> library/core/src/primitive_docs.rs:69:1
    |
    |
69  | / /// The `!` type, also called "never".
70  | | ///
71  | | /// `!` represents the type of computations which never resolve to any value at all. For example,
72  | | /// the [`exit`] function `fn exit(code: i32) -> !` exits the process without ever returning, and
...   |
264 | | /// [`Debug`]: fmt::Debug
265 | | /// [`default()`]: Default::default
    |
    = note: the link appears in this line:
            
            
            Default` for (eg.) [`File`] by just making [`default()`] panic.)
                                                        ^^^^^^^^^^^
    = help: to link to the module, prefix with `mod@`: mod@default
    = note: this link resolves to the module `default`, which is not in the value namespace
error: could not document `core`

Caused by:
Caused by:
  process didn't exit successfully: `/checkout/obj/build/bootstrap/debug/rustdoc --edition=2021 --crate-type lib --crate-name core library/core/src/lib.rs --target x86_64-unknown-linux-gnu -o /checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/doc -Zunstable-options --check-cfg 'names()' --check-cfg 'values()' --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat -Z unstable-options --resource-suffix 1.67.0 --markdown-css rust.css --markdown-no-toc --index-page /checkout/src/doc/index.md -C metadata=abbb49baeba48eac -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/release/deps --cfg=bootstrap -Csymbol-mangling-version=legacy -Zunstable-options -Zunstable-options '--check-cfg=values(bootstrap)' '--check-cfg=values(stdarch_intel_sde)' '--check-cfg=values(no_fp_fmt_parse)' '--check-cfg=values(no_global_oom_handling)' '--check-cfg=values(no_rc)' '--check-cfg=values(no_sync)' '--check-cfg=values(freebsd12)' '--check-cfg=values(backtrace_in_libstd)' '--check-cfg=values(target_env,"libnx")' '--check-cfg=values(target_os,"watchos")' '--check-cfg=values(target_arch,"asmjs","spirv","nvptx","nvptx64","le32","xtensa")' '--check-cfg=values(dont_compile_me)' '--check-cfg=values(rustix_use_libc)' --document-private-items -Dwarnings '-Wrustdoc::invalid_codeblock_attributes' --crate-version '1.67.0-nightly
  (26cee002e
  2022-11-06)' '-Zcrate-attr=doc(html_root_url="https://doc.rust-lang.org/nightly/")'` (exit status: 1)

//! vec![Edge { node: 1, cost: 1 },
//! Edge { node: 3, cost: 3 },
//! Edge { node: 4, cost: 1 }],
//! vec![Edge { node: 1, cost: 1 }, Edge { node: 3, cost: 3 }, Edge { node: 4, cost: 1 }],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is using rustc's rustfmt settings including use_small_heuristics = "Max". I think default settings would be more appropriate for library docs. But maybe that's getting too picky.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be tricky to do that without splitting the code blocks into their own files and include them (which I'm happy to try, if that would be an acceptable solution). An alternate solution would to instead add rustfmt file(s) for the rust library directories (but then it's applied across the libraries outside of doc comments). I don't think having specific format settings for docs is something that's even planned in rustfmt.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be tricky to do that without splitting the code blocks into their own files and include them (which I'm happy to try, if that would be an acceptable solution).

That would make updating documentation along with functions more tedious and easy to miss. Some macro-generated code already contains lots of annotations, an include could be easily missed.

An alternate solution would to instead add rustfmt file(s) for the rust library directories (but then it's applied across the libraries outside of doc comments).

That'd lead to reformatting all of the library code, no?

I don't think having specific format settings for docs is something that's even planned in rustfmt.

Well, if we decide that doc comment examples need different formatting then something like that might be necessary.

@scottmcm
Copy link
Member

Whether we want to do this sounds like a policy decision to me, so I'm going to flip this over to a new team member:
r? @the8472

@rustbot rustbot assigned the8472 and unassigned scottmcm Nov 10, 2022
@scottmcm scottmcm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 10, 2022
@bors
Copy link
Contributor

bors commented Nov 15, 2022

☔ The latest upstream changes (presumably #104418) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Member

@the8472 the8472 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether we want to do this sounds like a policy decision to me, so I'm going to flip this over to a new team member

Oh boy. What a terrible pick. I never had a good relationship with auto-formatters.

Soo... I don't think this makes sense unless it's enforced by tidy because otherwise there'll be drift and people occasionally doing drive-by format-the-world commits which are terrible for browsing git history.

And if we do enforce it via tidy then as @camsteffen mentions we might want a different policy for how doc comments are formatted vs. how internal code is formatted since they're displayed in different places (rustdoc with its width-limited layout vs. many different source code viewers). I have managed to avoid rustfmt enough that I don't even know enough about rustfmt.toml to make specific suggestions here.

Maybe if you think there are a few especially egregious comments that need fixing that could be done manually in a more targeted manner?

I'll see if other team members have opinions

Comment on lines -15 to -27
//! format!("Hello"); // => "Hello"
//! format!("Hello, {}!", "world"); // => "Hello, world!"
//! format!("The number is {}", 1); // => "The number is 1"
//! format!("{:?}", (3, 4)); // => "(3, 4)"
//! format!("{value}", value=4); // => "4"
//! format!("Hello"); // => "Hello"
//! format!("Hello, {}!", "world"); // => "Hello, world!"
//! format!("The number is {}", 1); // => "The number is 1"
//! format!("{:?}", (3, 4)); // => "(3, 4)"
//! format!("{value}", value = 4); // => "4"
//! let people = "Rustaceans";
//! format!("Hello {people}!"); // => "Hello Rustaceans!"
//! format!("{} {}", 1, 2); // => "1 2"
//! format!("{:04}", 42); // => "0042" with leading zeros
//! format!("{:#?}", (100, 200)); // => "(
//! // 100,
//! // 200,
//! // )"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was deliberately formatted that way for readability. rustfmt has some skip annotation that could help here.

Comment on lines 1892 to -1909
/// #![forbid(unsafe_code)] // with exclusive accesses,
/// // `UnsafeCell` is a transparent no-op wrapper,
/// // so no need for `unsafe` here.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this formatting may be somewhat questionable, but the rustfmt output is no better

//! vec![Edge { node: 1, cost: 1 },
//! Edge { node: 3, cost: 3 },
//! Edge { node: 4, cost: 1 }],
//! vec![Edge { node: 1, cost: 1 }, Edge { node: 3, cost: 3 }, Edge { node: 4, cost: 1 }],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be tricky to do that without splitting the code blocks into their own files and include them (which I'm happy to try, if that would be an acceptable solution).

That would make updating documentation along with functions more tedious and easy to miss. Some macro-generated code already contains lots of annotations, an include could be easily missed.

An alternate solution would to instead add rustfmt file(s) for the rust library directories (but then it's applied across the libraries outside of doc comments).

That'd lead to reformatting all of the library code, no?

I don't think having specific format settings for docs is something that's even planned in rustfmt.

Well, if we decide that doc comment examples need different formatting then something like that might be necessary.

@the8472
Copy link
Member

the8472 commented Nov 30, 2022

Ok, so I see you were previously directed to make the PR smaller. After having discussed it with the libs team there is at least some preference to have formatting but only if this automatically enforced via tidy and can be automatically applied via ./x fmt. #102285 (comment) indicated there were issues with rustfmt introducing trailing whitespaces.

I suggest to do the following

  • open an issue for broader discussion since this involves several parts (infra, rustfmt, libs and possibly other parts of the project)
  • fix rustfmt so it can be applied automatically without introducing trailing whitespaces or other problems
  • then if possible apply changes in smaller PRs on some directory trees by temporarily introducing intermediate rustfmt.toml (not sure if this works with ./x fmt, needs to be explored) or somehow coordinate a format-the-world event with review.

As you can see from a few of my review comments here, blindly applying auto-formatting without looking whether some manually arranged formatting should be preserved is also an issue and will need additional review, so it's unlikely that such a change will just be waved through.

I'll close this issue since we can't take it as-is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants